#include "actionqqlogin.h"
#include <Windows.h>
#include <QtGui>
#include <QtWidgets>
#define QQLWidth (495)
#define QQLHeight (470)

ActionQQLogin::ActionQQLogin()
{
    classname = __FUNCTION__;
    actParam = QJsonObject();
}

ExecuteRet ActionQQLogin::execute()
{
    HWND lastFound = NULL;
    HWND found = NULL;
    qInfo()<<classname<<"find QQLogin";
    do
    {
        found = ::FindWindowExA(NULL,lastFound,"TXGuiFoundation","QQ");
        if(found == NULL){
            qInfo()<<"not found TXGuiFoundation-QQ";
            return ERSucc;
        }

        if(TRUE == ::IsWindowVisible(found)){
            RECT rect;
            GetWindowRect(found,&rect);
            if(QQLWidth == rect.right-rect.left &&
                    QQLHeight == rect.bottom - rect.top)
            {
                actResult.insert("x",rect.left);
                actResult.insert("y",rect.top);
                actResult.insert("width",rect.right-rect.left);
                actResult.insert("height",rect.bottom-rect.top);
                SetForegroundWindow(found);

                QScreen* sc = QApplication::primaryScreen();
                QPixmap snap = sc->grabWindow(QApplication::desktop()->winId(),
                                              rect.left,rect.top,
                                              rect.right-rect.left,
                                              rect.bottom-rect.top);
                snap.save("D:\\temp.png");
                QImage simg = snap.toImage();
                QRgb c1 = simg.pixel(305,280); //普通登录界面用户账号输入槽
                qDebug()<<"c1:"<<c1;
                if(qRed(c1)==0xFF && qGreen(c1)==0xFF && qBlue(c1)==0xFF)
                {
                    return ERSub1;
                }
                QRgb c2 = simg.pixel(315,180); //验证码界面验证码输入槽内
                qDebug()<<"c2:"<<c2;
                if(qRed(c2)==0xFF && qGreen(c2)==0xFF && qBlue(c2)==0xFF)
                {
                    return ERSub2;
                }


                //QColor c1 = simg.pixel(80,145); //验证码界面盾牌左肩
                QRgb c31 = simg.pixel(85,155); //密码错误盾牌左中
                qDebug()<<"c31:"<<c31;
                if(qRed(c31)>245 && qGreen(c31)<220 &&
                        qGreen(c31)>100 && qBlue(c31)<100)
                {
                    return ERSub3;
                }
                QRgb c32 = simg.pixel(78,155); //找回密码盾牌左中
                qDebug()<<"c32:"<<c32;
                if(qRed(c32)>245 && qGreen(c32)<220 &&
                        qGreen(c32)>100 && qBlue(c32)<100)
                {
                    return ERSub4;
                }

                return ERSub5;
            }
        }
        lastFound = found;
    }
    while(found);

    return ERSucc;
}
